package com.noah.project.controller

//import com.noah.project.SpringAsyncConfig
import com.noah.project.global.Const
import com.noah.project.model.Comment
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import java.util.*
import java.util.Locale
import java.text.SimpleDateFormat
import org.thymeleaf.util.TextUtils


@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CommentApiControllerTest {
    @Autowired
    lateinit var commentController: CommentApiController
    /*@Autowired
    lateinit var springAsyncConfig: SpringAsyncConfig*/
    val log = LoggerFactory.getLogger(CommentApiController::class.simpleName)

    /*****************评论******************/
    @Test
    @Throws(Exception::class)
    fun addComment() {
        //val comment = Comment(uid = 1029, nid = 1, msg = "我觉得这个人相当之有嫌疑")
        for (i in 1..10) {
            val comment = Comment(uid = 2, nid = 1, msg = "测试第" + i + "条回复")
            val reply = commentController.addComment(comment, Const.TEST_TOKEN)
            println(reply.toString())
        }
    }

    @Test
    @Throws(Exception::class)
    fun deleteComment() {
        val reply = commentController.deleteComment(2, Const.TEST_TOKEN)
        println(reply.toString())
    }

    @Test
    @Throws(Exception::class)
    fun updateComment() {
        val comment = Comment(cid = 3, msg = "第一条弹幕")
        val reply = commentController.updateComment(comment, Const.TEST_TOKEN)
        println(reply.toString())
    }

    //日记下所有评论,带用户信息
    @Test
    @Throws(Exception::class)
    fun getNoteComments() {
        log.info("1")

        val reply = commentController.getCommentList(1, System.currentTimeMillis(), Const.TEST_TOKEN)
        log.info("2")
        println(reply.toString())
        /*if(reply.isDone){
            println(reply.toString())
        }*/
        /*val mRunnable = Runnable {
            run {
                Thread.sleep(2000)
                println(reply.toString())
            }
        }
        Thread(mRunnable).run()*/
    }

    //单条评论,带用户信息
    @Test
    @Throws(Exception::class)
    fun getComment() {
        val reply = commentController.getComment(3, Const.TEST_TOKEN)
        println(reply.toString())
    }

    //测试性能
    @Test
    @Throws(Exception::class)
    fun testAsync() {
        commentController.connect()
        Thread.sleep(500)
        log.info("开始时间:" + timeMillisecond(System.currentTimeMillis()))
        var max = 10000
        for (i in 1..max) {
            val reply = commentController.testThread()
            if (i > max-1) {
                println(reply)
                log.info("结束时间:" + timeMillisecond(System.currentTimeMillis()))
            }
        }

    }

    fun timeMillisecond(time: Long): String {
        val f = SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS", Locale.CHINA)
        return f.format(Date(time));
    }
}
